// pages/xcIndex/xcIndex.js
var xcIndexData = require("template.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    storeAddress: '雪宝乳业奶站-13589012060',//奶站
    news: "",//通知消息
    bannerImg: "",//广告轮播
    classif: [ //分类
      {
        "img": "img/index/classIf/xiannai.png",
        "flxh": "0",
        "text": "鲜奶"
      },
      {
        "img": "img/index/classIf/suannai.png",
        "flxh": "1",
        "text": "酸奶"
      },
      {
        "img": "img/index/classIf/hezhuang.png",
        "flxh": "2",
        "text": "盒装"
      },
      {
        "img": "img/index/classIf/pingzhuang.png",
        "flxh": "3",
        "text": "瓶装"
      },     
    ],
    cashCouponLqBtn: true,//代金券
    cashCouponLqBtnText: true,
    cashCouponCRText: "立即领取",
    cashCouponData:{//优惠券信息
      "price":"48.8",
      "text1": "优惠券",//单行
      "text2": "优惠券",
      "text3": "满399.00可用",
      "name":"天尚订",
    },
    cashCouponLq: '',//代金券列表信息
    initdata: '',//产品列表
    currentTab: 0,//分类默认下标
    tabBar:'',
    elasticFrameImg: "../../../images/index/tankuang.png",//20200312弹框图
    elasticFrameBtn: false,
    customerBtn: false,
    kefuBtn: true,
    liuyanBtn: true,
    customerNumber: 2,
    liuyanSuccess: true,
    textareaCustomer: true,
    textareaText: '',
    text: "这是一条测试公告，看看效果怎么样，2019年3月23日",
    marqueePace: 1,//滚动速度
    marqueeDistance: 0,//初始滚动距离
    marquee_margin: 30,
    size: 14,
    interval: 20 // 时间间隔
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var length = that.data.text.length * that.data.size;//文字长度
    var windowWidth = wx.getSystemInfoSync().windowWidth;// 屏幕宽度
    //console.log(length,windowWidth);
    that.setData({
       // news: xcIndexData.news,
      bannerImg: xcIndexData.bannerImg,
      cashCouponLq: xcIndexData.cashCouponLq,
      initdata: xcIndexData.initdata,
      tabBar: xcIndexData.tabBar,
      length: length,
      windowWidth: windowWidth
    });
    that.scrolltxt();// 第一个字消失后立即从右边出现

  },


  scrolltxt: function () {
    var that = this;
    var length = that.data.length;//滚动文字的宽度
    var windowWidth = that.data.windowWidth;//屏幕宽度
    if (length > windowWidth) {
      var interval = setInterval(function () {
        var maxscrollwidth = length + that.data.marquee_margin;//滚动的最大宽度，文字宽度+间距，如果需要一行文字滚完后再显示第二行可以修改marquee_margin值等于windowWidth即可
        var crentleft = that.data.marqueeDistance;
        if (crentleft < maxscrollwidth) {//判断是否滚动到最大宽度
          that.setData({
            marqueeDistance: crentleft + that.data.marqueePace
          })
        }
        else {
          //console.log("替换");
          that.setData({
            marqueeDistance: 0 // 直接重新滚动
          });
          clearInterval(interval);
          that.scrolltxt();
        }
      }, that.data.interval);
    }
    else {
      that.setData({ marquee_margin: "1000" });//只显示一条不滚动右边间距加大，防止重复显示
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  switchTab(event) { //20200219分类左右滑动切换
    var cur = event.detail.current;
    var singleNavWidth = this.data.windowWidth / (this.data.classif.lenght);
    this.setData({
      currentTab: cur,
      navScrollLeft: (cur - 2) * singleNavWidth
    });
  },
  //领悟优惠券
  couponLqBtnShow: function () {
    if (this.data.cashCouponLqBtnText) {
      wx.showToast({
        title: '领取成功',
        icon: 'success',
        duration: 500
      })
    }

    this.setData({
      cashCouponLqBtn: false,
      cashCouponLqBtnText: false,
      cashCouponCRText: "已领取"
    })
  },
  cashCouponLqAllHide: function () {
    this.setData({
      cashCouponLqBtn: true
    })
  },
  goClassif: function (event) {
    var page = this;
    if (this.data.currentTab == event.target.dataset.current) {
      return false;
    } else {
      page.setData({
        currentTab: event.target.dataset.current
      });
    }
  }, 
  //点击弹出
  customerBtn: function () {
    var customerBtn = !this.data.customerBtn
    this.setData({
      customerBtn: customerBtn
    })
  },
  kefuBtn: function () {

    var kefuBtn = !this.data.kefuBtn
    this.setData({
      kefuBtn: kefuBtn,
      customerBtn: false
    })
  },
  liuyanBtn: function () {
    var liuyanBtn = !this.data.liuyanBtn
    this.setData({
      liuyanBtn: liuyanBtn,
      customerBtn: false,
      textareaText: '',
      textareaCustomer: true,
    })
  },
  customerBtnHide: function () {
    this.setData({
      kefuBtn: true,
      liuyanBtn: true
    })
  },
  customerBtnSuccess: function () {
    this.setData({
      kefuBtn: true,
      liuyanBtn: true,
      liuyanSuccess: false,

    });
    this.customerDown()

  },
  customerBtnSuccessHide: function () {
    this.setData({
      liuyanSuccess: true
    });
  },
  customerDown() { //倒计时函数
    var that = this
    var customerDown = setInterval(function () {
      var customerNumber = that.data.customerNumber;
      console.log(customerNumber)
      if (customerNumber <= 1) {
        that.setData({
          liuyanSuccess: true,
          customerNumber: 2,

        })
        clearInterval(customerDown);
      } else {
        customerNumber--;
      }
      that.setData({
        customerNumber: customerNumber,
      })
    }, 1000);

  },
  textareaCustomer(e) {//吐槽
    this.data.textareaCustomer = e.detail.value.replace(/\s+/g, '');
    this.setData({
      textareaText: this.data.textareaCustomer
    });
    this.customerText()
  },
  customerText: function () {
    if (this.data.textareaCustomer) {
      this.setData({
        textareaCustomer: false,
        customerNumber: 2
      });
    } else {
      this.setData({
        textareaCustomer: true,
      });
    }
  }, 
  //弹框图显示，tabBar显示
  elasticFrameHide: function () {
    var elasticFrameBtn = this.data.elasticFrameBtn;
    elasticFrameBtn = true;
    this.setData({
      elasticFrameBtn: elasticFrameBtn
    })
    wx.showTabBar();//显示tabBar
  }, 
  goIndexDetails: function (event) {
    var newsid = event.currentTarget.dataset.newsid;
    console.log(newsid)
    wx.navigateTo({
      url: '../../../details/details?newsid=' + newsid
      // 跳转到商品详情
    })
  },  
  goTabBar: function (event) {
    var newsid = event.currentTarget.dataset.newsid;
    wx.redirectTo({
      url: newsid
      // 跳转tabBar
    })
  },  
})